package demoMod.invincibleOne.patches;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.evacipated.cardcrawl.modthespire.lib.*;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.helpers.FontHelper;
import com.megacrit.cardcrawl.screens.charSelect.CharacterOption;
import demoMod.invincibleOne.InvincibleOneMod;
import demoMod.invincibleOne.characters.InvincibleOne;
import demoMod.invincibleOne.skins.PrimeSkin;
import demoMod.invincibleOne.skins.Skin;

import java.lang.reflect.Field;

public class CharacterOptionPatch {
    public static final String[] TEXT = CardCrawlGame.languagePack.getUIString(InvincibleOneMod.makeID("SeriousLevel")).TEXT;

    @SpirePatch(
            clz = CharacterOption.class,
            method = "updateHitbox"
    )
    public static class PatchUpdateHitbox {
        @SpireInsertPatch(rloc = 81)
        public static void Insert(CharacterOption option) {
            if (Skin.skinNow().isPrimeForm && InvincibleOneMod.reskinUnlocked[CardCrawlGame.saveSlot]) {
                InvincibleOneMod.seriousLevel[CardCrawlGame.saveSlot] = 10;
                InvincibleOneMod.saveConfig();
            }
            if (option.selected && option.c instanceof InvincibleOne) {
                CardCrawlGame.mainMenuScreen.charSelectScreen.ascLevelInfoString =
                        TEXT[InvincibleOneMod.seriousLevel[CardCrawlGame.saveSlot]];
            }
        }
    }

    @SpirePatch(
            clz = CharacterOption.class,
            method = "incrementAscensionLevel"
    )
    public static class PatchIncrementAscensionLevel {
        public static SpireReturn<Void> Prefix(CharacterOption option, int level) {
            if (option.selected && option.c instanceof InvincibleOne) {
                if (Skin.skinNow().isPrimeForm && InvincibleOneMod.reskinUnlocked[CardCrawlGame.saveSlot]) {
                    return SpireReturn.Return(null);
                }
                InvincibleOneMod.seriousLevel[CardCrawlGame.saveSlot]++;
                if (InvincibleOneMod.seriousLevel[CardCrawlGame.saveSlot] > 10) {
                    InvincibleOneMod.seriousLevel[CardCrawlGame.saveSlot] = 10;
                }
                InvincibleOneMod.saveConfig();
                CardCrawlGame.mainMenuScreen.charSelectScreen.ascLevelInfoString = TEXT[InvincibleOneMod.seriousLevel[CardCrawlGame.saveSlot]];
                return SpireReturn.Return(null);
            }
            return SpireReturn.Continue();
        }
    }

    @SpirePatch(
            clz = CharacterOption.class,
            method = "decrementAscensionLevel"
    )
    public static class PatchDecrementAscensionLevel {
        public static SpireReturn<Void> Prefix(CharacterOption option, int level) {
            if (option.selected && option.c instanceof InvincibleOne) {
                if (Skin.skinNow().isPrimeForm && InvincibleOneMod.reskinUnlocked[CardCrawlGame.saveSlot]) {
                    return SpireReturn.Return(null);
                }
                InvincibleOneMod.seriousLevel[CardCrawlGame.saveSlot]--;
                if (InvincibleOneMod.seriousLevel[CardCrawlGame.saveSlot] < 0) {
                    InvincibleOneMod.seriousLevel[CardCrawlGame.saveSlot] = 0;
                }
                InvincibleOneMod.saveConfig();
                CardCrawlGame.mainMenuScreen.charSelectScreen.ascLevelInfoString = TEXT[InvincibleOneMod.seriousLevel[CardCrawlGame.saveSlot]];
                return SpireReturn.Return(null);
            }
            if (level < 0) {
                return SpireReturn.Return(null);
            }
            return SpireReturn.Continue();
        }
    };

    @SpirePatch(
            clz = CharacterOption.class,
            method = "renderInfo"
    )
    public static class CharacterDescPatch {
        @SpirePrefixPatch()
        public static void Prefix(CharacterOption _instance, SpriteBatch sb) {
            if (_instance.selected && _instance.c instanceof InvincibleOne) {
                try {
                    Field f = CharacterOption.class.getDeclaredField("flavorText");
                    f.setAccessible(true);
                    f.set(_instance, Skin.skinNow().desc);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
